<!-- 添加物料 -->
<template>
  <div class="add-material">
    <van-nav-bar @click-right="onClickRight" safe-area-inset-top style="background: #3e96e8">
      <template #title>
        <span style="color: #fff">添加物料</span>
      </template>
      <template #right>
        <span style="color: #fff" @click="save">保存</span>
      </template>
    </van-nav-bar>

    <!-- 表单 -->
    <div class="form">
      <van-form label-width="1rem">
        <van-field center colon readonly @click="typeshow = true" v-model="form.type" name="value" label="分类"
          right-icon="arrow-down" placeholder="请填写商品类别" />

        <van-field center colon name="datetime" v-model="form.brand" label="品牌" placeholder="请填写商品品牌" />

        <van-field center colon v-model="form.name" name="monmey" label="名称" placeholder="请填写商品名称" />
        <van-field center colon readonly @click="companyshow = true" v-model="form.unit" name="monmey" label="单位"
          right-icon="arrow-down" />
        <div class="spec">
          <van-field center colon v-model="form.spec" name="monmey" label="规格" />

        </div>
        <div class="spec">
          <van-field center colon v-model="form.weight" name="monmey" label="重量" />

        </div>

      </van-form>
      <div class="btns">
        <van-button name="2" type="primary" @click="cheng()">使用电子秤</van-button>
      </div>
    </div>

    <!-- 弹出层 -->
    <van-overlay :show="typeshow">
      <div class="wrapper" @click.stop>
        <div class="block">
          <van-cell-group>
            <van-cell title="分类" :title-style="{ color: '#3e96e8' }" />
            <!-- 循环 -->
            <van-cell :title="item" v-for="item in typelist" :key="item" @click="typeconfig(item)" />

            <van-cell title="取消" @click="typeshow = false" :title-style="{ color: '#3e96e8' }" />
          </van-cell-group>
        </div>
      </div>
    </van-overlay>


    <van-overlay :show="companyshow">
      <div class="wrapper" @click.stop>
        <div class="block">
          <van-cell-group>
            <van-cell title="单位" :title-style="{ color: '#3e96e8' }" />
            <!-- 循环 -->
            <van-cell :title="item" v-for="item in unitlist" :key="item" @click="companyconfig(item)" />

            <van-cell title="取消" @click="companyshow = false" :title-style="{ color: '#3e96e8' }" />
          </van-cell-group>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import Bus from '../../utils/bus.js'
import { getConfig } from "../../api/index";
export default {
  name: "",
  data() {
    return {
      form: {
        type: "",
        unit: "",
        weight: "",
      },
      companyshow: false,
      typeshow: false,
      specshow: false,
      type: "",
      value: "",
      name: "",
      company: "",
      spec: "",
      radio: "1",
      weight: "",
      config: {},
      typelist: [],
      unitlist: []
    };
  },
  destroyed() {
    Bus.$emit('addgoods', this.form)
  },
  methods: {
    companyconfig(value) {
      this.form.unit = value;
      this.companyshow = false;
    },
    typeconfig(value) {

      this.form.type = value;
      this.typeshow = false;
    },
    specconfig(value) { },
    onClickRight() { },
    save() {

      this.$router.back();

    },
    async init() {

      let config = await getConfig();
      if (config.status == 200) {
        var data = config.data;
        this.typelist = data.typelist.split(/[(\r\n)\r\n]+/);
        this.unitlist = data.unit.split(/[(\r\n)\r\n]+/);

      }
    },
    cheng() {
      try {
        window.ReactNativeWebView.BluetoothConn();
      } catch (eee) {

      }
    }
  },
  computed: {},
  watch: {},
  created() {


    this.init();
  },
  mounted() {
    var that = this;
    window.setWeight = function (data) {
      //that.getinfo(data);
      data = data.replace(/\s+/g, "");
      if (data.indexOf(".") != -1) {
        var num1 = data.substring(0, data.indexOf("."));
        var num2 = data.substring(data.indexOf(".") + 1);

        // alert(num1);
        // alert(num2);

        // var num3 = parseInt(num1);
        // alert(num3);
        that.form.weight = parseInt(num1) + "." + num2;

      } else {
        that.form.weight = data;
      }



    };


  },

};
</script>

<style lang="scss" scoped>
:deep(.van-cell__title) {
  text-align: center;
}

:deep(.van-field__body) {
  background-color: rgb(233, 248, 255);
  padding: 15px 10px;
}

.add-material {
  width: 100%;
  height: 100%;

  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }

  .block {
    margin-top: -200px;
    width: 420px;
    max-height: 620px;
    background-color: #fff;
    border-radius: 15px;
    overflow: hidden;
    overflow-y: scroll;
  }

  .form {
    width: 100%;
    background-color: #fff;
    padding-bottom: 50px;

    .spec {
      display: flex;
      align-items: center;

      .icon {
        background-color: rgb(66, 264, 209);
        margin-right: 30px;
      }

      .icon2 {
        background-color: rgb(249, 156, 121);
        margin-right: 30px;
      }
    }

    .btns {
      margin-top: 100px;
      width: 100%;
      display: flex;
      justify-content: space-around;
    }
  }
}
</style>